// Component 共同接口-->组合文件夹与文件
trait Component {
    fn search(&self, keyword: String);
}

struct Folder {
    name: String,
    components: Vec<Box<dyn Component>>,
}

struct File {
    name: String,
}

impl Component for Folder {
    fn search(&self, keyword: String) {
        println!("{} = in => {}", self.name, keyword);
        for x in self.components.iter() {
            x.search(keyword.clone());
        };
    }
}


impl Component for File {
    fn search(&self, keyword: String) {
        println!("file:{} =in=> {}", self.name, keyword)
    }
}

impl Folder {
    fn add(&mut self, rec: Box<dyn Component>) {
        self.components.push(rec);
    }
}

impl File {
    fn name(self) -> String {
        self.name
    }
}

#[test]
fn main() {
    let file = File { name: "a".to_string() };
    file.search(String::from("aa"));
    let folder = Folder { name: "Folder".to_string(), components: vec![Box::new(file)] };
    folder.search(String::from("folder"));
}